package com.ssu.judea.travlog;

import org.json.JSONException;
import org.json.JSONObject;

import com.ssu.judea.travlog.util.JSONParseHelper;
import com.ssu.judea.travlog.vo.Result;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class UserMentionActivity extends TravlogActivity implements OnClickListener{
	private EditText mensionContent;
	private TextView targetUser;
	private Button buttonWriteMension;
	private Button buttonCancel;
	private String currentTarget;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usermention);
		
		targetUser = (TextView)findViewById(R.id.textTargetUser);
		mensionContent = (EditText)findViewById(R.id.textMensionContent);
		
		buttonWriteMension = (Button)findViewById(R.id.buttonWriteToUser);
		buttonCancel = (Button)findViewById(R.id.buttonWriteCancel);
		
		buttonWriteMension.setOnClickListener(this);
		buttonCancel.setOnClickListener(this);
		
		currentTarget = getIntent().getAction();
		
		targetUser.setText("@"+currentTarget);
	}

	@Override
	public void onClick(View v) {
		if(v==buttonWriteMension){
			if(mensionContent.getText().toString().length()>0){
				String content = targetUser.getText().toString()+" "+mensionContent.getText().toString();
				getTravlogApp().doWriteMention(UserMentionActivity.this, content);
			}else{
				getTravlogApp().alert("Please type the content");
			}
		}else if(v==buttonCancel){
			finish();
		}
	}
	
	public void completeWriteMention(String resultStr){
		try {
			Result result = JSONParseHelper.parseToResult(new JSONObject(resultStr));
			getTravlogApp().alert(result.getMessage());
			finish();
		} catch (JSONException e) {
			Log.v("changdoc", e.getMessage());
			getTravlogApp().alert("Error!!\n"+e.getMessage());
		}
		
	}
}
